<template>
  <div class="table-page">
    <GiTable
      row-key="id"
      title="厂区信息管理"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #custom-left>
	    <a-input v-model="queryForm.name" placeholder="请输入" allow-clear @change="search">
	      <template #prefix><icon-search /></template>
	    </a-input>
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #custom-right>
        <a-button v-permission="['system:factorySite:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['system:factorySite:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['system:factorySite:list']" title="查看" @click="onDetail(record)">查看</a-link>
          <a-link v-permission="['system:factorySite:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['system:factorySite:delete']"
            status="danger"
            :disabled="record.disabled"
            title="删除"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <FactorySiteAddModal ref="FactorySiteAddModalRef" @save-success="search" />
    <FactorySiteDetailDrawer ref="FactorySiteDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import FactorySiteAddModal from './FactorySiteAddModal.vue'
import FactorySiteDetailDrawer from './FactorySiteDetailDrawer.vue'
import { type FactorySiteQuery, type FactorySiteResp, deleteFactorySite, exportFactorySite, listFactorySite } from '@/apis/meet/factorySite'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'FactorySite' })

const queryForm = reactive<FactorySiteQuery>({
  name: undefined,
  sort: ['createTime,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listFactorySite({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  { title: 'ID', dataIndex: 'id', slotName: 'id' },
  { title: '厂区名称', dataIndex: 'name', slotName: 'name' },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  // { title: '修改时间', dataIndex: 'updateTime', slotName: 'updateTime' },
  {
    title: '操作',
    slotName: 'action',
    width: 130,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['system:factorySite:update', 'system:factorySite:delete'])
  }
]

// 重置
const reset = () => {
  queryForm.name = undefined
  search()
}

// 删除
const onDelete = (record: FactorySiteResp) => {
  return handleDelete(() => deleteFactorySite(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportFactorySite(queryForm))
}

const FactorySiteAddModalRef = ref<InstanceType<typeof FactorySiteAddModal>>()
// 新增
const onAdd = () => {
  FactorySiteAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: FactorySiteResp) => {
  FactorySiteAddModalRef.value?.onUpdate(record.id)
}

const FactorySiteDetailDrawerRef = ref<InstanceType<typeof FactorySiteDetailDrawer>>()
// 详情
const onDetail = (record: FactorySiteResp) => {
  FactorySiteDetailDrawerRef.value?.onDetail(record.id)
}
</script>

<style lang="scss" scoped></style>
